package com.gt.quality.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.quality.base.BusinessSelfException;
import com.gt.quality.config.SpringBeanUtil;
import com.gt.quality.system.entity.SysFileInfo;
import com.gt.quality.system.mapper.SysFileInfoMapper;
import com.gt.quality.base.pojo.BaseLimitRequest;
import com.gt.quality.system.service.SysFileInfoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * (SysFileInfo)表服务实现类
 *
 * @author makejava
 * @since 2026-12-25 10:45:29
 */
@Slf4j
@Service("sysFileInfoService")
public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFileInfo> implements SysFileInfoService {

    @Resource
    private SysFileInfoMapper sysFileInfoMapper;

    @Value("${sys.conf.file.path}")
    private String filePath;

    @Value("${window.dev.file.path}")
    private String windowPath;


    /**
     * Description:  分页查询
     *
     * @param limitRequest 分页查询参数
     * @author: makeJava
     * @date: 2026-12-25 10:45:29

     */
    @Override
    public IPage<SysFileInfo> queryLimitPage(BaseLimitRequest<SysFileInfo> limitRequest) {
        SysFileInfo requestBody = limitRequest.getRequestBody();
        IPage<SysFileInfo> page = getByPageParam(limitRequest);
        JSONObject thisObj = JSONObject.from(requestBody);
        QueryWrapper<SysFileInfo> queryWrapper = queryWrapperBuilder(thisObj, limitRequest);
        return sysFileInfoMapper.selectPage(page, queryWrapper);
    }

    /**
     * Description:  批量新增优化版
     *
     * @param entityList entityList
     * @author: makeJava
     * @date: 2026-12-25 10:45:29
     * @return:Integer
     */
    @Override
    public Integer saveBatchByEasyBaseMapper(List<SysFileInfo> entityList) {

        return sysFileInfoMapper.insertBatchSomeColumn(entityList);
    }

    @Override
    public SysFileInfo saveFileInfo(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        SysFileInfo sysFileInfo;
        try {
            String env = System.getProperty("os.name").toLowerCase(Locale.ROOT);
            if (env.contains("win")) {
                filePath = windowPath;
            }
            String sysFilePath = this.getPathDefaultProxy(filePath) + originalFilename;
            File realFile = new File(sysFilePath);
            if (!realFile.exists()) {
                boolean result = realFile.mkdirs();
                log.info("文件创建--->{}:{}", filePath, result);
            }
            // 上传到服务器
            file.transferTo(realFile);
            // 保存到文件系统表
            assert originalFilename != null;
            sysFileInfo = new SysFileInfo().
                    setFilePath(sysFilePath).
                    setRemark(originalFilename).
                    setFileType(originalFilename.split("\\.")[1]);
            boolean save = this.save(sysFileInfo);
            log.info(save ? "文件保存成功!!!" : "上传保存失败!!!");
        } catch (IOException e) {
            log.error("文件上传失败....{}", e.getMessage(), e);
            throw new BusinessSelfException("文件上传失败....");
        }
        return sysFileInfo;
    }


    /**
     * 往上传递 SqlSessionFactory
     *
     * @return SqlSessionFactory
     */
    @Override
    public SqlSessionFactory getSqlSessionFactory() {
        return super.getSqlSessionFactory();
    }

    /**
     * 往上传递 Mapper
     *
     * @return Mapper
     */
    @Override
    public Class<?> getThisMapperClass() {
        return SysFileInfoMapper.class;
    }

    /**
     * 往上传递 Transaction
     *
     * @return Transaction
     */
    @Override
    public TransactionTemplate getSpringTx() {
        return SpringBeanUtil.getBean(TransactionTemplate.class);
    }
}


